﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmNhanvien : PRE.catalog.FrmBase
    {
        DAL.NhanvienDAL _dal = new DAL.NhanvienDAL();
        BLL.NhanvienBLL _bll = new BLL.NhanvienBLL();
        public FrmNhanvien()
        {
            InitializeComponent();
        }



        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);

            string _ma = _bll.CreateID();
            txtMA_NV.Text=_ma;

            txtTEN_NV.ResetText();
            txtDIACHI_NV.ResetText();
            txtSDT_NV.ResetText();
            dteNamsinh.EditValue = _bll.GetDate();
            txtTAIKHOAN.ResetText();
            txtMATKHAU.ResetText();
            cboMaCD.EditValue=-1;
            cboMaHV.EditValue=-1;
            cboquyen.ItemIndex = -1;
            txtTEN_NV.Focus();



            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtMA_NV.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            txtTEN_NV.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var name = Proper(txtTEN_NV.Text);
            var address = Proper(txtDIACHI_NV.Text);

            if (IsAdd)
            {
                var o = new DAL.Entities.nhanvien()
                {
                    ma_nv= txtMA_NV.Text,
                    ten_nv = name,
                    namsinh_nv = Convert.ToDateTime(dteNamsinh.EditValue),
                    gioitinh_nv = Convert.ToBoolean(radioGioitinh.EditValue),
                    diachi_nv = address,
                    sdt_nv = txtSDT_NV.Text,
                    taikhoan= txtTAIKHOAN.Text,
                    matkhau = txtMATKHAU.Text,

                    macd = (cboMaCD.EditValue + "").Trim(),
                    mahv= (cboMaHV.EditValue + "").Trim(),
                    vaitro=Convert.ToInt32(cboquyen.EditValue+"")

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                string _manv = txtMA_NV.Text;
                var o = new DAL.Entities.nhanvien()
                {
                    ma_nv = _manv.Trim(),
                    ten_nv = name,
                    namsinh_nv = Convert.ToDateTime(dteNamsinh.EditValue),
                    gioitinh_nv= Convert.ToBoolean(radioGioitinh.EditValue),
                    diachi_nv = address,
                    sdt_nv= txtSDT_NV.Text,
                    taikhoan= txtTAIKHOAN.Text,
                    matkhau= txtMATKHAU.Text,
                    macd = (cboMaCD.EditValue + "").Trim(),
                    mahv = (cboMaHV.EditValue + "").Trim(),
                     vaitro=Convert.ToInt32(cboquyen.EditValue+"")
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtMA_NV.Text = null;
            txtDIACHI_NV.Text = null;
            txtSDT_NV.Text = null;
            txtTEN_NV.Text = null;
            dteNamsinh.EditValue = DateTime.Now;
            radioGioitinh.EditValue = true;
            txtTAIKHOAN.Text = null;
            txtMATKHAU.Text = null;
            cboMaCD.EditValue=-1;
            cboMaHV.EditValue = -1;
            cboquyen.ItemIndex = -1;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMA_NV.DataBindings.Clear();
            txtTEN_NV.DataBindings.Clear();
            dteNamsinh.DataBindings.Clear();
            radioGioitinh.DataBindings.Clear();
            txtDIACHI_NV.DataBindings.Clear();
            txtSDT_NV.DataBindings.Clear();
            txtTAIKHOAN.DataBindings.Clear();
            txtMATKHAU.DataBindings.Clear();
            cboMaCD.DataBindings.Clear();
            cboMaHV.DataBindings.Clear();
            cboquyen.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMA_NV.DataBindings.Add("EditValue", _dtb, ".MA_NV");
            txtTEN_NV.DataBindings.Add("EditValue", _dtb, ".TEN_NV");
            dteNamsinh.DataBindings.Add("EditValue", _dtb, ".Namsinh_NV");
            radioGioitinh.DataBindings.Add("EditValue", _dtb, ".Gioitinh_NV");
            txtDIACHI_NV.DataBindings.Add("EditValue", _dtb, ".Diachi_NV");
            txtSDT_NV.DataBindings.Add("EditValue", _dtb, ".sdt_NV");
            txtTAIKHOAN.DataBindings.Add("EditValue", _dtb, ".taikhoan");
            txtMATKHAU.DataBindings.Add("EditValue", _dtb, ".matkhau");
            cboMaCD.DataBindings.Add("EditValue", _dtb, ".MACD");
            cboquyen.DataBindings.Add("EditValue", _dtb, ".Vaitro");
            cboMaHV.DataBindings.Add("EditValue", _dtb, ".MAHV");
            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtMA_NV.Properties.ReadOnly = isReadOnly;
            txtTEN_NV.Properties.ReadOnly = isReadOnly;
            dteNamsinh.Properties.ReadOnly = isReadOnly;
            radioGioitinh.Properties.ReadOnly = isReadOnly;
            txtDIACHI_NV.Properties.ReadOnly = isReadOnly;
            txtSDT_NV.Properties.ReadOnly = isReadOnly;
            txtTAIKHOAN.Properties.ReadOnly = isReadOnly;
            txtMATKHAU.Properties.ReadOnly = isReadOnly;
            cboMaCD.Properties.ReadOnly = isReadOnly;
            cboMaHV.Properties.ReadOnly = isReadOnly;
            cboquyen.Properties.ReadOnly = isReadOnly;
            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
                //tong so nhan vien hien tai
                label1.Text = "Tổng số nhân viên hiện tại  có : " + Convert.ToString(_dtb.Rows.Count)+" nhân viên";
            }

            base.LoadData();
        }
        #endregion


        private void FrmNhanvien_Load_1(object sender, EventArgs e)
        {
            DataTable _dtb1 = _dal.Select_macd();
            cboMaCD.Properties.DataSource = _dtb1;

            DataTable _dtb2 = _dal.Select_mahv();
            cboMaHV.Properties.DataSource = _dtb2;
            cboquyen.Properties.DataSource = _dal.Select_quyen();

            PerformRefresh();
        }

       

        private void dockPanel1_Click(object sender, EventArgs e)
        {

        }

        private void dockPanel2_Click(object sender, EventArgs e)
        {

        }

        private void grcMain_Click(object sender, EventArgs e)
        {

        }
    }
}
